#!/bin/bash

# 性能对比测试脚本
# 比较单容器多服务和传统多容器的性能差异

echo "🚀 开始性能对比测试..."
echo "=================================="

# 测试函数
test_mysql_performance() {
    local container_name=$1
    local test_name=$2
    
    echo "📊 测试 $test_name MySQL性能..."
    
    # 测试连接速度
    start_time=$(date +%s%N)
    for i in {1..100}; do
        mysql -h localhost -P 3306 -u root -p123456 -e "SELECT 1;" > /dev/null 2>&1
    done
    end_time=$(date +%s%N)
    
    connection_time=$(( (end_time - start_time) / 1000000 ))
    echo "  连接测试: 100次连接耗时 ${connection_time}ms"
    
    # 测试查询性能
    start_time=$(date +%s%N)
    mysql -h localhost -P 3306 -u root -p123456 -e "
        CREATE DATABASE IF NOT EXISTS perf_test;
        USE perf_test;
        CREATE TABLE IF NOT EXISTS test_table (
            id INT AUTO_INCREMENT PRIMARY KEY,
            name VARCHAR(100),
            value INT,
            created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
        );
        INSERT INTO test_table (name, value) VALUES 
        ('test1', 1), ('test2', 2), ('test3', 3), ('test4', 4), ('test5', 5);
        SELECT COUNT(*) FROM test_table;
        DROP DATABASE perf_test;
    " > /dev/null 2>&1
    end_time=$(date +%s%N)
    
    query_time=$(( (end_time - start_time) / 1000000 ))
    echo "  查询测试: 复杂查询耗时 ${query_time}ms"
}

test_redis_performance() {
    local container_name=$1
    local test_name=$2
    
    echo "📊 测试 $test_name Redis性能..."
    
    # 测试连接速度
    start_time=$(date +%s%N)
    for i in {1..100}; do
        redis-cli -h localhost -p 6379 -a 123456 ping > /dev/null 2>&1
    done
    end_time=$(date +%s%N)
    
    connection_time=$(( (end_time - start_time) / 1000000 ))
    echo "  连接测试: 100次连接耗时 ${connection_time}ms"
    
    # 测试读写性能
    start_time=$(date +%s%N)
    for i in {1..1000}; do
        redis-cli -h localhost -p 6379 -a 123456 SET "key$i" "value$i" > /dev/null 2>&1
        redis-cli -h localhost -p 6379 -a 123456 GET "key$i" > /dev/null 2>&1
    done
    end_time=$(date +%s%N)
    
    operation_time=$(( (end_time - start_time) / 1000000 ))
    echo "  读写测试: 1000次操作耗时 ${operation_time}ms"
    
    # 清理测试数据
    redis-cli -h localhost -p 6379 -a 123456 FLUSHDB > /dev/null 2>&1
}

test_resource_usage() {
    local container_name=$1
    local test_name=$2
    
    echo "📊 测试 $test_name 资源使用情况..."
    
    # 获取容器资源使用情况
    local stats=$(docker stats --no-stream --format "table {{.CPUPerc}}\t{{.MemUsage}}\t{{.MemPerc}}" $container_name | tail -1)
    
    if [ ! -z "$stats" ]; then
        echo "  CPU使用: $stats"
    else
        echo "  无法获取资源使用情况"
    fi
}

# 启动多服务容器
echo "🔧 启动多服务容器..."
cd docker/multi-service
docker-compose up -d

# 等待容器启动
echo "⏳ 等待容器启动..."
sleep 30

# 测试多服务容器性能
echo ""
echo "🧪 测试多服务容器性能..."
test_mysql_performance "multi-service-demo" "多服务容器"
test_redis_performance "multi-service-demo" "多服务容器"
test_resource_usage "multi-service-demo" "多服务容器"

# 停止多服务容器
echo ""
echo "🛑 停止多服务容器..."
docker-compose down

# 启动传统多容器
echo ""
echo "🔧 启动传统多容器..."
docker run -d --name mysql-test \
    -e MYSQL_ROOT_PASSWORD=123456 \
    -p 3307:3306 \
    mysql:5.7

docker run -d --name redis-test \
    -p 6380:6379 \
    redis:7-alpine redis-server --requirepass 123456

# 等待容器启动
echo "⏳ 等待传统容器启动..."
sleep 30

# 测试传统容器性能
echo ""
echo "🧪 测试传统多容器性能..."
test_mysql_performance "mysql-test" "传统MySQL容器"
test_redis_performance "redis-test" "传统Redis容器"
test_resource_usage "mysql-test" "传统MySQL容器"
test_resource_usage "redis-test" "传统Redis容器"

# 清理测试容器
echo ""
echo "🧹 清理测试容器..."
docker stop mysql-test redis-test
docker rm mysql-test redis-test

echo ""
echo "✅ 性能对比测试完成！"
echo "=================================="
echo ""
echo "📋 测试总结："
echo "1. 多服务容器：一个容器运行多个服务"
echo "2. 传统多容器：每个服务独立容器"
echo ""
echo "💡 建议："
echo "- 开发环境：可以使用多服务容器节省资源"
echo "- 生产环境：推荐使用传统多容器架构"
echo "- 性能要求高：根据实际测试结果选择" 